<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Repos\RoleRepo;
use App\Repos\DepartRepo;
use App\Services\Wechat;
use App\Models\Worktype;
use App\Models\Wxuser;
use Illuminate\Pagination\LengthAwarePaginator;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $query = User::query(); 
        if($request->role_code) {
            $where['role_code'] = $request->role_code;
            $query->where($where);
        }

        if($request->keyword) {
            $where['keyword'] = $request->keyword;
           // $query->where('truename','like','%'.$where['keyword'].'%')
        }

        $list    = $query->paginate(20);
        $roles   = RoleRepo::getRoles();
       
        return view('admin.user.index',compact('list','roles','where'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {   
       
        $roles = RoleRepo::getRoles();
        $openids = User::pluck('openid');
        $wxUsers = Wxuser::whereNotIn('openid',$openids)->whereNotNull('openid')->get();
        $smallUsers = Wxuser::whereNotNull('small_openid')->get();
        return view('admin.user.add',compact('roles','wxUsers','smallUsers'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token','password2']);
        $salt = $string = str_random(40);
        $data['password'] = md5($data['password'].$salt);
        $data['salt']     = $salt;  
       
        $flag = User::create($data);
        if($flag) {
            return redirect('zadmin/users');
        }
        return back()->withInput();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data       = User::find($id);
        $roles      = RoleRepo::getRoles();
        $departs    = DepartRepo::get();
        $wxUsers    = Wxuser::whereNotNull('openid')->get();
        $smallUsers = Wxuser::whereNotNull('small_openid')->get();
        return view('admin.user.edit',compact('data','roles','departs','smallUsers','wxUsers'));
    }

     public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method','password2','password');
        if($request->password){
            $salt = str_random(40);
            $data['password'] = md5($request->password.$salt);
            $data['salt']     = $salt; 
        }
        
        $rs   = User::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/users');
        }
        return back()->withInput();
    }

     /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = User::destroy($id);
        if ($rs) {
            return redirect('zadmin/users');
        }
        return back();
    }

  
}
